package com.orafl.use.comorafluse.definition;


import com.orafl.use.comorafluse.IOC.Lookup.LookUpBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.StringUtils;

import java.util.Map;

import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;

/**
 * 通过bean注册方式
 */
public class definitionContextMain {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext  context = new AnnotationConfigApplicationContext();
        BeanDefinitionBuilder builder = genericBeanDefinition(KingUser.class);
        builder.addPropertyValue("user","至尊宝");
        BeanDefinitionRegistry registry = context;
        String beanName = "adc";
        if(StringUtils.hasText(beanName)){
            registerDefinistion(registry,builder,beanName);
        }else {
            registerDefinistion(registry, builder);
        }

        Map<String, KingUser> map = context.getDefaultListableBeanFactory().getBeansOfType(KingUser.class);
        System.out.println(map);
    }

    public static void registerDefinistion(BeanDefinitionRegistry registry,BeanDefinitionBuilder builder,String beanName){
        registry.registerBeanDefinition(beanName,builder.getBeanDefinition());
    }

    public static void registerDefinistion(BeanDefinitionRegistry registry,BeanDefinitionBuilder builder){
        BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(),registry);
    }
}
